// 云函数入口文件
const cloud = require('wx-server-sdk')
var rp = require('request-promise');
const TcbRouter = require('tcb-router');
cloud.init({
	env: cloud.DYNAMIC_CURRENT_ENV
})
const db = cloud.database()
const _ = db.command

// 云函数入口函数
exports.main = async (event, context) => {
	const app = new TcbRouter({
		event
	})
	app.use(async (ctx, next) => {
		ctx.data = {}
		ctx.wxContext = cloud.getWXContext()
		await next()
	})
	app.router('unifiedOrder', async (ctx, next) => {
		let orderId = (Date.now()).toString()
		await db.collection('test').add({
			data:{
				orderId
			}
		})
		const res = await cloud.cloudPay.unifiedOrder({
		    "body" : "微飒云商城",
		    "outTradeNo" : orderId,
		    "spbillCreateIp" : "127.0.0.1",
		    "subMchId" : "1592295751",
		    "totalFee" : 1,
		    "envId": 'wxsaas-pmp-0g3z2n0o9b665704',
		    "functionName": "payBack"
		  })
		  
	  ctx.body = res 
	})
	return app.serve()
}
